package com.sakura.probe.component;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class MyBatisPlusHandler implements MetaObjectHandler {

    //默认头像位置
    private final String defaultHeadImg = "https://probe-oss.oss-cn-beijing.aliyuncs.com/probe/personinfo/default.jpeg";


    //插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("mybatis-plus start insert fill...");
        //MyBatisPlus在进行插入时会自动将创建时间和修改时间填充进去
        this.setFieldValByName("headImg",defaultHeadImg,metaObject);
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("lastEditTime",new Date(),metaObject);
    }
    //修改时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("mybatis-plus start update fill...");
        //MyBatisPlus在进行修改操作时只对updateTime进行填充
        this.setFieldValByName("lastEditTime",new Date(),metaObject);
    }
}
